package com.sk.cardeal.act.manage.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.sk.cardeal.R;
import com.sk.cardeal.act.manage.act.InActivity;
import com.sk.cardeal.adapter.InListAdapter;
import com.sk.cardeal.base.BaseFragment;
import com.sk.cardeal.callback.HandleDataCallBack;
import com.sk.cardeal.entity.InListData;
import com.sk.cardeal.entity.PostIn;
import com.sk.cardeal.utils.AppApi;
import com.sk.cardeal.utils.MyUtils;
import com.sk.cardeal.view.MultiStateView;
import com.sk.cardeal.view.SimpleMultiStateView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;
import in.srain.cube.views.ptr.PtrClassicFrameLayout;
import in.srain.cube.views.ptr.PtrDefaultHandler;
import in.srain.cube.views.ptr.PtrFrameLayout;
import in.srain.cube.views.ptr.PtrHandler;

/**
 * Created by sk on 2018/3/9.
 */

public class InFragment extends BaseFragment implements BaseQuickAdapter.OnItemClickListener, HandleDataCallBack {
    @BindView(R.id.ed_search)
    EditText edSearch;
    @BindView(R.id.data_ryc)
    RecyclerView dataRyc;
    @BindView(R.id.add)
    ImageView add;
    PostIn postIn = new PostIn();

    List<InListData.ResultListBean> dataList = new ArrayList<>();
    @BindView(R.id.SimpleMultiStateView)
    SimpleMultiStateView simpleMultiStateView;
    @BindView(R.id.mPtrFrameLayout)
    PtrClassicFrameLayout mPtrFrameLayout;
    Unbinder unbinder;
    private InListAdapter adapter;
    private boolean isRefresh = false;


    @Override
    protected int getContentView() {
        return R.layout.fragment_in;
    }


    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        initStateView();

        mPtrFrameLayout.disableWhenHorizontalMove(true);

        mPtrFrameLayout.setPtrHandler(new PtrHandler() {
            @Override
            public boolean checkCanDoRefresh(PtrFrameLayout frame, View content, View header) {
                return PtrDefaultHandler.checkContentCanBePulledDown(frame, dataRyc, header);
            }

            @Override
            public void onRefreshBegin(PtrFrameLayout frame) {
                isRefresh = true;
                doRefresh();
            }
        });

        dataRyc.setLayoutManager(new LinearLayoutManager(mContext));
        adapter = new InListAdapter(R.layout.item_in_list, dataList);
        dataRyc.setAdapter(adapter);

        View emptyView = LayoutInflater.from(mContext).inflate(R.layout.empty_view, null);
        TextView textView = emptyView.findViewById(R.id.content);
        textView.setText("暂无任何信息哦!");
        adapter.setEmptyView(emptyView);

        adapter.setOnItemClickListener(this);
        postIn.setUserid(MyUtils.getUser(mContext).getId());

        AppApi.getInOrderList(postIn, this, 0, getActivity());
    }

    private void doRefresh() {
        AppApi.getInOrderList(postIn, this, 0, getActivity());
    }

    @OnClick(R.id.add)
    public void onViewClicked() {

    }

    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
        Intent intent = new Intent(mContext, InActivity.class);
        intent.putExtra("id",dataList.get(position).getId());
        intent.putExtra("code",dataList.get(position).getCode());
        startActivity(intent);
    }

    @Override
    public void handlerSuccessData(String result, int tag) {
         showSuccess();
         if(isRefresh){
             isRefresh = false;
             mPtrFrameLayout.refreshComplete();
             dataList.clear();
         }
         InListData inListData = JSON.parseObject(result,InListData.class);
         dataList.addAll(inListData.getResultList());
         adapter.notifyDataSetChanged();
    }

    @Override
    public void handlerFailData(String failString, int tag) {

    }

    @Override
    public void handleErrorData(String error, int tag) {

    }

    private void initStateView() {
        if (simpleMultiStateView == null) return;
        simpleMultiStateView.setEmptyResource(R.layout.view_empty)
                .setRetryResource(R.layout.view_retry)
                .setLoadingResource(R.layout.view_loading)
                .setNoNetResource(R.layout.view_nonet)
                .build()
                .setonReLoadlistener(new MultiStateView.onReLoadlistener() {
                    @Override
                    public void onReload() {
                    }
                });
    }

    public void showLoading() {
        if (simpleMultiStateView != null) {
            simpleMultiStateView.showLoadingView();
        }
    }

    public void showSuccess() {
        if (simpleMultiStateView != null) {
            simpleMultiStateView.showContent();
        }
    }

    public void showFaild() {
        if (simpleMultiStateView != null) {
            simpleMultiStateView.showErrorView();
        }
    }

    public void showNoNet() {
        if (simpleMultiStateView != null) {
            simpleMultiStateView.showNoNetView();
        }
    }
}
